package com.cfh.web.json;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;

import com.cfh.domain.User;
import com.cfh.service.UserService;
import com.cfh.util.Tools;
import com.opensymphony.xwork2.ActionSupport;

public class ChangePasswordJson extends ActionSupport implements SessionAware {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(ChangePasswordJson.class);

	private static final long serialVersionUID = 1L;
	private UserService userService;
	private String oldPassword;
	private String newPassword;
	private String newPasswordCheck;

	private Map root;
	private Map session;

	public void setSession(Map map) {
		session = map;
	}

	public String execute() {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}

		root = new HashMap();
		User currentUser = (User) session.get("currentUser");
		if (currentUser != null && Tools.hasText(getOldPassword())
				&& currentUser.getPASSWORD().equals(oldPassword)
				&& Tools.hasText(getNewPassword())
				&& Tools.hasText(getNewPasswordCheck())
				&& getNewPassword().equals(getNewPasswordCheck())) {
			currentUser.setPASSWORD(newPassword);
			userService.changePassword(currentUser, newPassword);

			getRoot().put("success", new Boolean(true));
			if (logger.isDebugEnabled()) {
				logger.debug("execute() - end"); //$NON-NLS-1$
			}
			return SUCCESS;

		}

		getRoot().put("success", new Boolean(false));
		// ArrayList errors = new ArrayList(1);
		// Map error1 = new HashMap();
		// error1.put("id", "pass");
		// error1.put("msg", "用户名或密码错误");
		// errors.add(error1);
		// getRoot().put("errors", errors);

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	public Map getRoot() {
		return root;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setNewPasswordCheck(String newPasswordCheck) {
		this.newPasswordCheck = newPasswordCheck;
	}

	public String getNewPasswordCheck() {
		return newPasswordCheck;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getOldPassword() {
		return oldPassword;
	}

}
